package com.hst.capacity.domain.service.impl;


import com.alibaba.fastjson2.JSON;
import com.hst.capacity.application.biz.FranchiseeBiz;
import com.hst.capacity.domain.common.constants.Constants;
import com.hst.capacity.domain.common.constants.HashMapConstants;
import com.hst.capacity.domain.exception.BusinessException;
import com.hst.capacity.domain.model.entity.invoice.InvoiceEntity;
import com.hst.capacity.domain.model.entity.order.pool.OrderPoolEntity;
import com.hst.capacity.domain.model.request.invoice.InvoiceLstParam;
import com.hst.capacity.domain.model.request.invoice.InvoiceParam;
import com.hst.capacity.domain.model.request.specialcar.SpListParam;
import com.hst.capacity.domain.model.response.PagingResultVO;
import com.hst.capacity.domain.model.response.invoice.InvoiceVO;
import com.hst.capacity.domain.service.InvoiceService;
import com.hst.capacity.infra.convertor.InvoiceConvertor;
import com.hst.capacity.infra.persistence.jpa.invoice.InvoiceRepository;
import com.hst.capacity.infra.persistence.mybatis.InvoiceMapper;
import com.hst.capacity.infra.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class InvoiceServiceImpl implements InvoiceService {

    @Autowired
    InvoiceRepository invoiceRepository;

    @Autowired
    InvoiceMapper invoiceMapper;

    @Autowired
    FranchiseeBiz franchiseeBiz;

    /**
     * 分页并条件查询发票信息
     * @param param
     * @return
     */
    @Override
    public PagingResultVO<InvoiceVO> searchWithConditionsAndPaging(InvoiceLstParam param) {
        List<InvoiceVO> list = invoiceMapper.searchWithConditionsAndPaging(param);
        for (InvoiceVO invoiceVO : list) {
            String city = HashMapConstants.idFranchiseeCityMap.get(invoiceVO.getIdFranchisee());
            city = StringUtil.isNotBlank(city) ? city : "-";
            invoiceVO.setCity(city);
        }
        return new PagingResultVO<>(invoiceMapper.searchWithConditionsAndPagingTotal(param), list);
    }

    /**
     * 查询发票信息(通过发票ID)
     * @param idInvoice
     * @return
     */
    @Override
    public InvoiceVO findByIdInvoice(String idInvoice) {
        return InvoiceConvertor.INSTANCE.convert2VO(invoiceRepository.findByIdInvoice(idInvoice));
    }

    /**
     * 查询发票信息(通过订单ID)
     * @param idOrder
     * @return
     */
    @Override
    public InvoiceVO findByIdOrder(String idOrder) {
        return InvoiceConvertor.INSTANCE.convert2VO(invoiceRepository.findByIdInvoice(idOrder));
    }

    /**
     * 查询发票信息(通过三方订单ID)
     * @param idExternalOrder
     * @return
     */
    @Override
    public InvoiceVO findByExternalOrderId(String idExternalOrder) {
        return InvoiceConvertor.INSTANCE.convert2VO(invoiceRepository.findByExternalOrderId(idExternalOrder));
    }

    /**
     * 开票（更新发票记录）
     * 1，行程记录表
     * 2，关联电子发票
     * 3，发邮件
     * @param param
     * @return
     */
    @Override
    public String updInvoiceInfo(InvoiceParam param) {
        InvoiceEntity entity = invoiceRepository.findByIdInvoice(param.getIdInvoice());
        if (entity == null)
            throw new BusinessException(5001, "未找到此发票记录");

        if (StringUtil.isNotBlank(param.getInvoiceUrl())) {
            entity.setInvoiceUrl(param.getInvoiceUrl());
            entity.setInvoiceStatus(Constants.InvoiceStatus.INVOICE_DONE);
        }

        if (param.getInvoiceAmount() != null) {
            entity.setInvoiceAmount(param.getInvoiceAmount());
        }

        if (param.getIsCompanyInvoice() != null) {
            entity.setIsCompanyInvoice(param.getIsCompanyInvoice());
        }

        if (StringUtil.isNotBlank(param.getInvoiceContent())) {
            entity.setInvoiceContent(param.getInvoiceContent());
        }

        if (StringUtil.isNotBlank(param.getInvoiceTitle())) {
            entity.setInvoiceTitle(param.getInvoiceTitle());
        }

        if (StringUtil.isNotBlank(param.getTaxNo())) {
            entity.setTaxNo(param.getTaxNo());
        }

        if (StringUtil.isNotBlank(param.getCompanyAddress())) {
            entity.setCompanyAddress(param.getCompanyAddress());
        }

        if (StringUtil.isNotBlank(param.getCompanyPhone())) {
            entity.setCompanyPhone(param.getCompanyPhone());
        }

        if (StringUtil.isNotBlank(param.getCompanyBank())) {
            entity.setCompanyBank(param.getCompanyBank());
        }

        if (StringUtil.isNotBlank(param.getCompanyBankNo())) {
            entity.setCompanyBankNo(param.getCompanyBankNo());
        }

        entity = invoiceRepository.saveAndFlush(entity);
        return entity.getIdInvoice();
    }

    /**
     * 开票作废
     * @param param
     * @return
     */
    @Override
    public String invalidInvoice(InvoiceParam param) {
        InvoiceEntity entity = invoiceRepository.findByIdInvoice(param.getIdInvoice());
        if (entity == null)
            throw new BusinessException(5001, "未找到此发票记录");
        entity.setInvoiceStatus(Constants.InvoiceStatus.INVOICE_INVALID);
        entity = invoiceRepository.saveAndFlush(entity);
        return entity.getIdInvoice();
    }

    @Override
    public List<InvoiceVO> findAllInvoiceLst(InvoiceLstParam param) {
        String idFranchiseeArr = franchiseeBiz.findIdFranArrByFinance(param.getIdFinance());
        if (StringUtil.isNotBlank(idFranchiseeArr)) {
            param.setIdFranchiseeArr(idFranchiseeArr);
            param.setIdFranchisee(null);
        }
        log.info("InvoiceLstParam :{}", JSON.toJSONString(param));
        return invoiceMapper.findAllInvoiceLst(param);
    }

    @Override
    public void updInvoicePdfUrl(String invoiceUrl, String idInvoice) {
        invoiceMapper.updInvoicePdfUrl(invoiceUrl, Constants.InvoiceStatus.INVOICE_DONE, idInvoice);
    }

}
